// Copyright (c) 2010, Anthony Cassidy
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are 
// permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice, this list 
//         of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice, this 
//         list of conditions and the following disclaimer in the documentation and/or 
//         other materials provided with the distribution.
//     * Neither the name of the AntsAdventureGameCreator nor the names of its contributors 
//         may be used to endorse or promote products derived from this software without 
//         specific prior written permission.
// 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
// THE POSSIBILITY OF SUCH DAMAGE.
#include "XStdafx.h"
#include "XChoicesSlotCollection.h"


XChoicesSlotCollection::XChoicesSlotCollection()
{
    m_itemOfHighlightedSlot = -1;
}

void XChoicesSlotCollection::Clear()
{ 
    m_rects.clear();
}

int XChoicesSlotCollection::Count()
{ 
    return m_rects.size();
}

void XChoicesSlotCollection::SetHighlightedSlot(int indexOfHighlightedSlot)
{
    m_itemOfHighlightedSlot = indexOfHighlightedSlot;
}


void XChoicesSlotCollection::DrawSlotIfVisible(int i, QPainter& displayPainter, QPainter& zbufferPainter, QRgb zbufferColor, bool forceRolloverOff)
{
    if(i == m_itemOfHighlightedSlot && !forceRolloverOff)
    {
        displayPainter.drawImage(0,0, m_rolloverImages[i]);
    }
    else
    {
        displayPainter.drawImage(0,0,m_images[i]);
    }
    zbufferPainter.fillRect( m_rects[i], QBrush(zbufferColor));
}

bool XChoicesSlotCollection::IsMouseOverSlot(int i, QPoint pos)
{
    return m_rects[i].contains(pos);
}

void XChoicesSlotCollection::TriggerSlot(int index)
{
    (index=index);
}

void WipeCanvas(QImage& image);



QImage  GenerateImageForChoice(QString textPassedIn, QRect rect, const QColor& colorPassedIn)
{
    QImage image(rect.right(), rect.bottom(), QImage::Format_ARGB32_Premultiplied);

    //always wipe canvas before painter
    WipeCanvas(image);

    QPainter painter;
    bool isOk = painter.begin(&image);
    if(isOk)
    {
        QFont font;
        painter.setFont(font);

        // draw black bg
        QBrush blackBrush(Qt::black);
        painter.fillRect(rect, blackBrush);

        // if rollover, draw text in yellow
        painter.setPen(colorPassedIn);
        painter.drawText(rect, Qt::AlignLeft|Qt::TextSingleLine , textPassedIn);

        bool isOk2 = painter.end();
        ASSERT(isOk2 && "painter failed in XVerbsGui");
        isOk2 = isOk2;
    }
    return image;
}

void XChoicesSlotCollection::UpdateSlotImage(int i, QString text)
{
    QImage plain = GenerateImageForChoice(text, m_rects[i], Qt::green);
    QImage rollover = GenerateImageForChoice(text, m_rects[i], Qt::yellow);

    if(m_images.size() <= i)
    {    
        m_images.resize(i+1);
        m_rolloverImages.resize(i+1);
    }

    m_images[i] = plain;
    m_rolloverImages[i] = rollover; 
}


void XChoicesSlotCollection::ResizeRectList(QRect rect, QRect& rectForUpArrow, QRect& rectForDownArrow)
{
    QFont font;
    QFontMetrics fm(font);
    fm.height();

    int arrowWidth = fm.height();
    int arrowHeight = fm.height();
    int textHeight = fm.height();
    int gap = 2; // gap between scroll bar and text
    int visibleTextLines = rect.height() / fm.height();


    // initialize image 
    int resultantWidthOfTextLine = rect.width()-arrowWidth;

    // finally, set the rectangles
    int startingYCoord = rect.y();
    int startingXCoord = rect.x() + arrowWidth + gap;
    int lastBottomCoord = startingYCoord;

    m_rects.clear();
    for(int i=0;i<visibleTextLines;i++)
    {
        QRect rect(startingXCoord,lastBottomCoord+1, resultantWidthOfTextLine, textHeight);
        m_rects.push_back(rect);
        lastBottomCoord = lastBottomCoord + fm.height();
    }

    rectForUpArrow = QRect(m_rects.front().left()-arrowWidth-gap,  m_rects.front().top(), arrowWidth, arrowHeight);
    rectForDownArrow = QRect(m_rects.back().left()-arrowWidth-gap,  m_rects.back().top(), arrowWidth, arrowHeight);
}
